<!--日期范围选择组件2-->
<template>
  <a-form-item :label="label">
    <a-range-picker
        :style="getStyle"
        v-decorator="vDecorator"
        :disabled="isDisabled"
        :format="format"
        @change="change"
    ></a-range-picker>
  </a-form-item>
</template>

<script>
import { mixin } from './js/form.js'

export default {
  name: 'AntFormRangePicker',
  mixins: [mixin],
  props: {
    /**
     * 是否禁用
     */
    disabled: {
      type: Boolean,
      default: false
    },
    /**
     * 是否由form表单自动布局
     */
    autoLayoutByForm: {
      type: Boolean,
      default: true
    },
    /**
     * 显示文本
     */
    label: {
      type: String,
      default: ''
    },
    /**
     * 输入提示
     */
    placeholder: {
      type: String,
      default: ''
    },
    /**
     * 开始时间绑定字段 ['phone' ,{ rules: [{ required: true},{validator:function(rule, value, callback)] }],自定义验证方法时callback回调函数必须在方法中调用，
     * 失败返回错误提示callback("xxx")，成功返回callback()
     */
    vDecorator: {
      type: Array,
      default: () => []
    },
    /**
     * 结束时间绑定字段 ['phone' ,{ rules: [{ required: true},{validator:function(rule, value, callback)] }],自定义验证方法时callback回调函数必须在方法中调用，
     * 失败返回错误提示callback("xxx")，成功返回callback()
     */
    vDecorator2: {
      type: Array,
      default: () => []
    },
    /**
     * 日期格式，YYYY/MM/DD DD/MM/YYYY YYYY/MM...
     */
    format: {
      type: String,
      default: 'YYYY-MM-DD'
    },
    /**
     * css样式，优先级最高
     */
    cstyle: {
      type: String,
      default: ''
    },
    /**
     * 非自动布局的宽度，优先级最低
     */
    defaultWidth: {
      type: Number,
      default: 240
    }
  },
  data() {
    return {
      //是否禁用和输入提示，由AntForm组件调用
      cdisabled: false,
      cplaceholder: this.placeholder ? this.placeholder : '请选择' + this.label
    }
  },
  computed: {
    /**
     * 获取样式
     */
    getStyle() {
      return { width: (this.percentageLayoutWidth ? this.percentageLayoutWidth : this.defaultWidth) + 'px', ...this.cstyle }
    }
  },
  created() {
  },
  mounted() {
    // 自动添加验证为空时的message
    let vDecorator = this.vDecorator
    if (vDecorator.length > 1 && vDecorator[1].rules) {
      let rules = vDecorator[1].rules
      rules.forEach(obj => {
        if (obj.required && !obj.message) {
          obj.message = '请选择' + this.label
        }
      })
    }
  },
  watch: {},
  beforeDestroy() {
  },
  methods: {
    /**
     * 输入内容改变事件
     * @param value
     * @param data
     */
    change(value, option) {
      this.$emit('change', value, option)
    },
    /**
     * 赋值之前的转换操作,由AntForm组件自动调用
     * 参数：recordPara，转换之前的record
     * 返回值：转换之后的可用于赋值的对象
     */
    beforeSetValue(recordPara) {
      let record = {}
      let arr = []
      // 开始日期
      let key = this.vDecorator[0]
      let val = recordPara[key]
      val = val == null ? null : this.$moment(val, this.format)
      arr.push(val)
      // 结束日期
      let key2 = this.vDecorator2[0]
      let val2 = recordPara[key2]
      val2 = val2 == null ? null : this.$moment(val2, this.format)
      arr.push(val2)
      // 构造record
      record[key] = arr
      return record
    },
    /**
     * 取值之前的转换操作,由AntForm组件自动调用
     * 参数：recordPara，转换之前的record
     * 返回值：转换之后的对象
     */
    beforeGetValue(recordPara) {
      let record = {}
      let key = this.vDecorator[0]
      let key2 = this.vDecorator2[0]
      let arr = recordPara[key]
      if (!arr) {
        arr = [null, null]
      }
      // 开始日期
      let val = arr[0]
      val = !val ? null : val.format(this.format)
      record[key] = val
      // 结束日期
      let val2 = arr[1]
      val2 = !val2 ? null : val2.format(this.format)
      record[key2] = val2
      return record
    }
  }
}
</script>

<style lang="less" scoped>
::v-deep .ant-calendar-picker-input.ant-input {
  padding-left: 0px;
}

</style>
